package com.myCommunity.ui;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCommunity.dao.impl.TopicDaoImpl;
import com.myCommunity.entity.Topic;
import com.myCommunity.entity.Users;

public class AfterSearchServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Users user = (Users) request.getSession().getAttribute("user");

		request.setAttribute("user", user);
		
		String title = request.getParameter("title");
		
		int pageNum = 1 ; 
		int pageSize = 5;
		if(request.getParameter("pageNum") != null && !request.getParameter("pageNum").isEmpty())
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		
		int row = new TopicDaoImpl().getTopicListByTitle(0, 0, title).size();
		int totalPage = (int)Math.ceil((double)row/pageSize);
		
		List<Topic> t = new TopicDaoImpl().getTopicListByTitle(pageNum, pageSize, title);
		
		request.setAttribute("row", row);
		request.setAttribute("topic", t);
		request.setAttribute("title", title);
		request.setAttribute("pageNum",pageNum);
		request.setAttribute("totalPage", totalPage);
		
		request.getRequestDispatcher("afterSearch.jsp").forward(request, response);
	}

}
